<?php
require_once '../includes/functions.php';
check_login();

// 获取模板列表
$templates_dir = __DIR__ . '/../templates';
$templates = array_filter(scandir($templates_dir), function($item) use ($templates_dir) {
    return $item !== '.' && $item !== '..' && is_dir($templates_dir . '/' . $item);
});

// 获取落地页列表和统计数据
$landing_pages_dir = __DIR__ . '/../landing_pages';
if (!file_exists($landing_pages_dir)) {
    mkdir($landing_pages_dir, 0777, true);
}
$landing_pages = array_filter(scandir($landing_pages_dir), function($item) use ($landing_pages_dir) {
    return $item !== '.' && $item !== '..' && is_dir($landing_pages_dir . '/' . $item);
});

// 获取统计数据
$db = db_connect();
$stats = [];
$sql = "SELECT * FROM page_stats";
$result = $db->query($sql);
if ($result) {
    while ($row = $result->fetch(PDO::FETCH_ASSOC)) {
        $stats[$row['page_id']] = $row;
    }
}
?>
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>管理后台 - 落地页管理系统</title>
    <link rel="stylesheet" href="css/admin.css">
</head>
<body>
    <div class="admin-container">
        <div class="sidebar">
            <div class="sidebar-header">
                <h2>落地页管理系统</h2>
            </div>
            <ul class="sidebar-menu">
                <li><a href="index.php">仪表盘</a></li>
                <li><a href="create_subdomain.php">创建落地页</a></li>
                <li><a href="logout.php">退出登录</a></li>
            </ul>
        </div>
        
        <div class="main-content">
            <div class="header">
                <h1>仪表盘</h1>
                <div class="user-info">
                    欢迎, <?php echo htmlspecialchars($_SESSION['username']); ?>
                    <div class="user-dropdown">
                        <a href="profile.php">个人信息</a>
                        <div class="divider"></div>
                        <a href="logout.php">退出登录</a>
                    </div>
                </div>
            </div>
            
            <div class="content">
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">模板列表</h2>
                        <a href="template_editor.php" class="btn btn-success">新建模板</a>
                    </div>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>模板名称</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($templates as $template): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($template); ?></td>
                                <td class="action-buttons">
                                    <a href="template_editor.php?template=<?php echo urlencode($template); ?>" class="btn">编辑</a>
                                    <a href="create_subdomain.php?template=<?php echo urlencode($template); ?>" class="btn btn-success">使用此模板</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
                
                <div class="card">
                    <div class="card-header">
                        <h2 class="card-title">落地页列表</h2>
                        <a href="create_subdomain.php" class="btn btn-success">新建落地页</a>
                    </div>
                    <table class="table">
                        <thead>
                            <tr>
                                <th>二级域名</th>
                                <th>完整域名</th>
                                <th>总访问量</th>
                                <th>总点击量</th>
                                <th>今日访问</th>
                                <th>今日点击</th>
                                <th>操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($landing_pages as $subdomain): ?>
                            <tr>
                                <td><?php echo htmlspecialchars($subdomain); ?></td>
                                <td><?php echo htmlspecialchars($subdomain . '.jl25.net'); ?></td>
                                <td><?php echo isset($stats[$subdomain]) ? $stats[$subdomain]['total_visits'] : 0; ?></td>
                                <td><?php echo isset($stats[$subdomain]) ? $stats[$subdomain]['total_clicks'] : 0; ?></td>
                                <td><?php echo isset($stats[$subdomain]) ? $stats[$subdomain]['today_visits'] : 0; ?></td>
                                <td><?php echo isset($stats[$subdomain]) ? $stats[$subdomain]['today_clicks'] : 0; ?></td>
                                <td class="action-buttons">
                                    <a href="edit_subdomain.php?subdomain=<?php echo urlencode($subdomain); ?>" class="btn">编辑</a>
                                    <a href="http://<?php echo htmlspecialchars($subdomain . '.jl25.net'); ?>" target="_blank" class="btn btn-success">访问</a>
                                    <a href="delete_subdomain.php?subdomain=<?php echo urlencode($subdomain); ?>" class="btn btn-danger" onclick="return confirm('确定要删除此落地页吗？')">删除</a>
                                </td>
                            </tr>
                            <?php endforeach; ?>
                        </tbody>
                    </table>
                </div>
            </div>
        </div>
    </div>
</body>
</html>
